/**
 * Created by zhengrunqiang on 2016-12-21.
 */
myApp.controller("StepCtrl", ['$scope', '$http', '$routeParams', '$restUrls', '$post', '$rootScope', function ($scope, $http, $routeParams, $restUrls, $post, $rootScope) {
   $scope.OneStep = true;
    $scope.CountryList = [];
    $scope.ZoneList = [];
    $scope.DistrictList = [];
    $scope.Userinfo = {
        lastname: '',
        telephone: '',
        addressformat: '',
        choose_country: {},
        choose_zone: {},
        choose_district:{},
        hardware_code:'',
        hardware_name:'',
        buy_address:'',
        buy_time:'',
        network_id:0
    };
    $scope.Childinfo = {
        name: '',
        birthday: '',
        choose_grade: {},
        school:''
    };
   //获取省级数据
    $scope.GetCountryList = function () {
        $http({ method: "GET", url: $restUrls["getCountry"], params: {} }).success(function (result) {
            if (result.success == 1) {
                $scope.CountryList = result.data;
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    //选择省
    $scope.ChooseCountry = function (country) {
        $scope.ProvinceShow = false;
        $scope.CityShow = true;
        $scope.Userinfo.choose_country = country;
        $http({ method: "GET", url: $restUrls["getZone"], params: { country_id: country.country_id } }).success(function (result) {
            if (result.success == 1) {
                $scope.ZoneList = result.data;
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    //选择市
    $scope.ChooseZone = function (zone) {
        $scope.ProvinceShow = false;
        $scope.CityShow = false;
        $scope.CountryShow = true;
        $scope.Userinfo.choose_zone = zone;
        $http({ method: "GET", url: $restUrls["getDistrict"], params: { zone_id: zone.zone_id } }).success(function (result) {
            if (result.success == 1) {
                $scope.DistrictList = result.data;
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    //选择区
    $scope.ChooseDistrict = function (district) {
        $scope.ProvinceShow = false;
        $scope.CityShow = false;
        $scope.CountryShow = false;
        $scope.AddressShow = false;
        $scope.Userinfo.choose_district = district;
        $scope.Userinfo.addressformat = $scope.Userinfo.choose_country.name + $scope.Userinfo.choose_zone.name + $scope.Userinfo.choose_district.name;

    }


    $scope.Grades = [
        {
            time: new Date().getFullYear() - 1 + '-09-01',
            name: '小学一年级'
        },
        {
            time: new Date().getFullYear() - 2 + '-09-01',
            name: '小学二年级'
        },
        {
            time: new Date().getFullYear() - 3 + '-09-01',
            name: '小学三年级'
        },
        {
            time: new Date().getFullYear() - 4 + '-09-01',
            name: '小学四年级'
        },
        {
            time: new Date().getFullYear() - 5 + '-09-01',
            name: '小学五年级'
        },
        {
            time: new Date().getFullYear() - 6 + '-09-01',
            name: '初中一年级'
        },
        {
            time: new Date().getFullYear() - 7 + '-09-01',
            name: '初中二年级'
        },
        {
            time: new Date().getFullYear() - 8 + '-09-01',
            name: '初中三年级'
        }
    ]
    //获取用户绑定的孩子

    var currYear = (new Date()).getFullYear();
    var opt = {};
    opt.date = { preset: 'date' };
    opt.datetime = { preset: 'datetime' };
    opt.time = { preset: 'time' };
    opt.default = {
        theme: 'android-ics light', //皮肤样式
        display: 'modal', //显示方式
        mode: 'scroller', //日期选择模式
        dateFormat: 'yyyy-mm-dd',
        lang: 'zh',
        // showNow: true,
        // nowText: "今天",
        startYear: currYear - 70, //开始年份
        endYear: currYear //结束年份
    };
    $("#appDate").mobiscroll($.extend(opt['date'], opt['default']));

    $("#appDate2").mobiscroll($.extend(opt['date'], opt['default']));

    $scope.GetCountryList();

    //选择年级
    $scope.ChooseGrade = function (grade) {
        $scope.gradeshow = false;
        $scope.Childinfo.choose_grade = grade;
    }


    //第一步保存
    $scope.sumbitone= false;
    $scope.EditCustomerInfoOne = function () {
        if ($scope.sumbitone) {
            return;
        }
        if (!$scope.OneForm.$valid) {
            $scope.showOneError = true;
            if(!$scope.Userinfo.lastname){
                $.scojs_message('家长姓名必填', $.scojs_message.TYPE_ERROR);
            }
            else if(!$scope.Userinfo.addressformat){
                $.scojs_message('家长地址必填', $.scojs_message.TYPE_ERROR);
            }
            else if(!$scope.Childinfo.name){
                $.scojs_message('孩子姓名必填', $.scojs_message.TYPE_ERROR);
            }
            else if(!$scope.Childinfo.birthday){
                $.scojs_message('孩子出生日期必填', $.scojs_message.TYPE_ERROR);
            }
            else if(!$scope.Childinfo.school){
                $.scojs_message('孩子学校必填', $.scojs_message.TYPE_ERROR);
            }else{
                $.scojs_message('家长电话号码格式不正确', $.scojs_message.TYPE_ERROR);
            }
            return;
        }
        if($scope.Userinfo.hardware_code && $scope.Userinfo.hardware_code.length>0){
            if(!$scope.isBarcodeNo($scope.Userinfo.hardware_code)){
                $.scojs_message('机身编码不合规则', $.scojs_message.TYPE_ERROR);
                return;
            }
        }

        var parameters = {
            lastname: $scope.Userinfo.lastname,
            hardware_code:$scope.Userinfo.hardware_code||'',
            telephone: $scope.Userinfo.telephone || '',
            country_id :$scope.Userinfo.choose_country.country_id,
            zone_id: $scope.Userinfo.choose_zone.zone_id || '',
            district_id:$scope.Userinfo.choose_district.district_id,
            name:$scope.Childinfo.name,
            birthday:$scope.Childinfo.birthday,
            grade:$scope.Childinfo.choose_grade.time,
            school:$scope.Childinfo.school,
            sort:1,
            hardware_name:$scope.Userinfo.hardware_name,
            buy_time:$scope.Userinfo.buy_time,
            buy_address:$scope.Userinfo.buy_address,
            network_id:$scope.Userinfo.network_id

        };
        $scope.sumbitone = true;
        $post($restUrls["editCustomerInfoOne"], parameters).success(function (result) {
            if (result.success == 1) {
                $scope.sumbitone = false;
                $scope.OneStep = false;
                $scope.TwoStep = true;
            }
            else {
                $scope.sumbitone = false;
                $.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
            }
        }).error(function (data, status, headers, config) {
            $scope.sumbitone = false;
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        })
    }


    $scope.recommend = {
        telephone:''
    }
    //用户信息第二步(可再获得20积分)
    $scope.sumbittwo = false;
    $scope.EditCustomerInfoTwo = function (info) {
        if ($scope.sumbittwo) {
            return;
        }
        if (!$scope.TwoForm.$valid) {
            $scope.showTwoError = true;
            return;
        }
        var parameters = {
            telephone:info.telephone
        }
        $scope.sumbittwo = true;
        $post($restUrls["editCustomerInfoTwo"], parameters).success(function (result) {
            if (result.success == 1) {
                $scope.sumbittwo = false;
                window.location.href ="#/usercenter";
            } else {
                $scope.sumbittwo = false;
                $.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
            }
        }).error(function (data, status, headers, config) {
            $scope.sumbittwo = false;
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }


    $scope.isBarcodeNo = function (code) {
        if(code.length == 14){
            //表示第一位是数字，第二位是字母，从第三位到第十四位为数字
            return code.match(/\d{1}[a-lA-L]\d{12}/g);
        }else if(code.length== 9||code.length == 10||code.length== 11){
            //表示校验规则为全是数字
            var reg = new RegExp("^[0-9]*$");
            console.log(reg);
            return reg.test(code);
        }else{
            return false;
        }
    }


    $scope.getProductCateInfo = function () {
        $http({ method: "GET", url: $restUrls["getProductCateInfo"], params: {  } }).success(function (result) {
            if (result.success == 1) {
                $scope.categorys = result.data;
            }
        }).error(function (data, status, headers, config) {
        });
    }
    $scope.getProductCateInfo();
    
    $scope.ChooseCategory = function (category) {
        $scope.products = category.productDetailList;
        $scope.categoryshow=false;
        $scope.productshow=true;
    }
    $scope.ChooseProduct = function (product) {
        $scope.Userinfo.hardware_name = product.product_name;
        $scope.productshow=false;
        $scope.productmodelshow = false;
    }


    $scope.getAreaInfo = function () {
        $http({ method: "GET", url: $restUrls["getAreaInfo"], params: { id:0 } }).success(function (result) {
            if (result.success == 1) {
                $scope.areas = result.data;
            }
        }).error(function (data, status, headers, config) {
        });
    }
    $scope.getAreaInfo();
    
    $scope.ChooseArea = function (area) {
        $http({ method: "GET", url: $restUrls["getAreaInfo"], params: { id:area.areaid } }).success(function (result) {
            if (result.success == 1) {
                $scope.area2s = result.data;
                $scope.areashow = false;
                $scope.areashow2 = true;
            }
        }).error(function (data, status, headers, config) {
        });
    }

    $scope.ChooseArea2= function (area) {
        $http({ method: "GET", url: $restUrls["getAreaInfo"], params: { id:area.areaid } }).success(function (result) {
            if (result.success == 1) {
                $scope.area3s = result.data;
                $scope.areashow2 = false;
                $scope.areashow3 = true;
            }
        }).error(function (data, status, headers, config) {
        });
    }
    $scope.ChooseArea3 = function (area) {
        $http({ method: "GET", url: $restUrls["getNetworkInfo"], params: { areaid:area.areaid } }).success(function (result) {
            if (result.success == 1) {
                $scope.networks = result.data;
                $scope.areashow3 = false;
                $scope.areashow4 = true;
            }
        }).error(function (data, status, headers, config) {
        });
    }

    $scope.ChooseNetWork = function (network) {
       $scope.Userinfo.buy_address = network.network_name;
       $scope.Userinfo.network_id = network.id;
       $scope.areashow4 = false;
       $scope.networkmodelshow = false;
    }


  // 弹出层禁止页面滚动
  var pagetop=null;
  // 启动弹出层
  $scope.showpop=function(){
    pagetop = $(window).scrollTop();
    $('body').css("top", -pagetop + "px"); //给body一个负的top值；
    $('body').addClass('fixed'); //给body增加一个类，position:fixed; 
  }

  $scope.closepop=function(){
    $('body').removeClass('fixed'); //去掉给body的类
    $('body').removeAttr("style");//去掉内联样式
    $(window).scrollTop(pagetop); //设置页面滚动的高度，如果不设置，关闭弹出层时页面会回到顶部。
    pagetop=null;
  }
}]);
